<?php

class DNewsletterType extends DActiveRecord {

    /**
     * Supplemental variables
     */
    private $_lang_id;
    protected $restrictedId = array(1, 2, 3, 4); //những ID không cho update hoặc delete

    /**
     * Construct
     */

    public function __construct($scenario = 'insert') {
        parent::__construct($scenario);

        $this->_lang_id = 1;
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'newsletter_type';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('title', 'required'),
            array('parent_id, activated_flag', 'numerical', 'integerOnly' => true),
            array('title, title_seo', 'length', 'max' => 255),
//			array('id', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'parent_id'      => 'Cấp cha',
            'title'          => 'Tiêu đề',
            'activated_flag' => 'Hiện',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        $criteria = new CDbCriteria(array(
                'order' => 'sort_order DESC',
            ));

        $criteria->compare('id', $this->id);
        $criteria->compare('parent_id', $this->parent_id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('title_seo', $this->title_seo, true);
        $criteria->compare('activated_flag', $this->activated_flag);
        return new CActiveDataProvider($this, array(
                'criteria' => $criteria,
            ));
    }

    /**
     * Before save
     */
    protected function beforeSave() {
        parent::beforeSave();
        $this->setDefaultValue();

        return true;
    }

    /**
     * Update user/time Sets
     */
    private function setDefaultValue() {
        if ($this->getIsNewRecord())
            $this->setAttribute('sort_order', $this->getAutoIncrement($this->tableName()));

        $this->setAttribute('title_seo', $this->getTitleSeo());

        return true;
    }

    private function getTitleSeo($title = '') {
        if (empty($title))
            $title = $this->title;

        $myhelpers = new MyHelpers();
        return $myhelpers->convertSeoStr($title);
    }

    /**
     * @param boolean $active
     * @param boolean $filter If filter is true will only get 'Static content & Newsletter'
     * @return array $result['parent_id']['id'] = $title
     */
    public function getMenu($active = '', $filter=true) {
        $criteria = new CDbCriteria(array(
                'order' => 'sort_order ASC'
            ));
        $criteria->addCondition('language_id = ' . $this->_lang_id);
        if ($active != '') {
            $criteria->compare('activated_flag', $active);
        }

        $rows   = $this->findAll($criteria);
        $result = array();

        foreach ($rows as $row)
            $result[$row['parent_id']][$row['id']] = $row['title'];
        
        if ($filter) {
            unset($result['0']['3']); //products
        }

        return $result;
    }

    /**
     * Get childs of Parent Menu
     * @param integer $parentMenu
     * @return array
     */
    public function getMenuChild($parent_id) {
        $criteria = new CDbCriteria(array(
                'order' => 'sort_order ASC',
            ));
        $criteria->compare('parent_id', $parent_id);
        $criteria->addCondition('language_id = ' . $this->_lang_id);

        $rows   = $this->findAll($criteria);
        $result = array();

        foreach ($rows as $row)
            $result[$row['id']] = $row['title'];

        return $result;
    }

    /**
     * Check the menu which can delete
     * @param string $menu 
     * @return boolean
     */
    public function canDelete($menuId) {
        $child = $this->getMenuChild($menuId);
        if (empty($child))
            return true;
        else
            return false;
    }

    /**
     * ========================== OPTIONS =================================
     */
    public function getActivatedOption() {
        return array(
            '0' => 'No',
            '1' => 'Yes',
        );
    }

    public function getActivatedText($key = '') {
        $options = $this->getActivatedOption();

        if (empty($key))
            return $options[$this->activated_flag];
        else
            return $options[$key];
    }

    /**
     * 
     * @param integer $activated_flag
     * @return array $row['id'] = $title 
     */
    public function getParentIdOption($activated_flag = '') {
        $criteria = new CDbCriteria(array(
                'order' => 'sort_order DESC',
            ));
        $criteria->addCondition('language_id = ' . $this->_lang_id);

        if (!empty($activated_flag)) //filter activated_flag
            $criteria->compare('activated_flag', $activated_flag);

        $curScenario = $this->getScenario();
        if ($curScenario == 'update')
            $criteria->addCondition('id != ' . $this->id);

        $rows   = $this->findAll($criteria);
        $result = array();

        foreach ($rows as $row)
            $result[$row['id']] = $row['title'];

        return $result;
    }

    public function getTypeText($id) {
        $opts = $this->getParentIdOption();
        return $opts[$id];
    }

    /**
     * ========================== END OF OPTIONS ==========================
     */
}